package aobing;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * VolatileTest
 *
 * @author Syzer
 * @Description VolatileTest
 * @Date 2021/6/16 11:43
 */
public class VolatileTest {

    static class Aobing extends Thread{
        private boolean flag = false;

        ThreadLocal<Integer> threadLocal = new ThreadLocal<>();

        public boolean isFlag() {
            return flag;
        }

        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            flag = true;
            System.out.println("flag=" + flag);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Aobing aobing = new Aobing();
        AtomicInteger atomicInteger = new AtomicInteger(0);
        aobing.start();
        while(true) {
            synchronized (aobing) {
                int andIncrement = atomicInteger.getAndIncrement();
                aobing.threadLocal.set(andIncrement);
                if (aobing.isFlag()) {
                    System.out.println("有点东西" + aobing.threadLocal.get());
                }
            }
        }
    }
}
